package com.mashibing.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.mashibing.bean.FcBuilding;
import com.mashibing.bean.FcEstate;
import com.mashibing.bean.TblCompany;
import com.mashibing.returnJson.ReturnObject;
import com.mashibing.service.EstateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true",
        methods = {}, allowedHeaders = "*")
public class EstateController {

    @Autowired
    private EstateService estateService;

    @RequestMapping("/estate/selectCompany")
    public String selectCompany() {
        System.out.println("selectCompany");
        List<TblCompany> tblCompanies = estateService.selectCompany();
        System.out.println(tblCompanies);
        return JSONObject.toJSONString(new ReturnObject(tblCompanies));
    }

    @RequestMapping("/estate/insertEstate")
    public String insertEstate(FcEstate fcEstate) {
        System.out.println("insert estate");
        Integer integer = estateService.insertEstate(fcEstate);
        if (integer == 0) {
            return JSONObject.toJSONString(new ReturnObject("0", "房产编码已经存在"));
        } else {
            return JSONObject.toJSONString(new ReturnObject("1", "插入房产成功"));
        }
    }

    /*此处完成的是楼宇的查询功能，但是现在数据表中没有任何楼宇数据
     * 因此在编写的时候需要进行插入且返回插入的数据
     * */
    @RequestMapping("/estate/selectBuilding")
    public String selectBuilding(Integer buildingNumber, String estateCode) {
        System.out.println("select building:" + buildingNumber + "----" + estateCode);
        List<FcBuilding> fcBuildings = estateService.selectBuilding(buildingNumber, estateCode);
        System.out.println(fcBuildings);
        return JSONObject.toJSONString(new ReturnObject(fcBuildings));
    }

    @RequestMapping("/estate/updateBuilding")
    public String updateBuilding(FcBuilding fcBuilding) {
        System.out.println("update building");
        Integer result = estateService.updateBuilding(fcBuilding);
        if (result == 1) {
            return JSONObject.toJSONString(new ReturnObject("更新楼宇成功"));
        } else {
            return JSONObject.toJSONString(new ReturnObject("更新楼宇失败"));
        }
    }
}
